package com.senyint.hip.fhir.web;

import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.impl.GenericClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.senyint.hip.fhir.interceptor.CaptureInterceptor;
import com.senyint.hip.fhir.util.DateUtils;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
import org.springframework.web.bind.annotation.*;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author ChenPan
 * @date 2018/10/30 11:39
 */
@RestController
@RequestMapping("/encounter")
public class EncController extends FhirBaseController {

    @PostMapping()
    public String createEnc(@RequestBody String data) throws FHIRException {
        Encounter enc = convertEnc(data);

        CaptureInterceptor interceptor = new CaptureInterceptor();
        GenericClient client = newClient(tempData.getBaseServer(), interceptor);

        MethodOutcome out = client.create().resource(enc).execute();

        return new Gson().toJson(out);

    }

    @PutMapping("/{id}")
    public String updateEnc(@PathVariable("id") String id, @RequestBody String data) throws FHIRException {
        Encounter enc = convertEnc(data);

        CaptureInterceptor interceptor = new CaptureInterceptor();
        GenericClient client = newClient(tempData.getBaseServer(), interceptor);

        MethodOutcome out = client.update(id, enc);

        return new Gson().toJson(out);

    }

    protected Encounter convertEnc(String data) {
        JSONObject jsonObject = JSON.parseObject(data);

        Encounter enc = new Encounter();

        Narrative narrative = new Narrative();
        narrative.setStatus(Narrative.NarrativeStatus.GENERATED)
                .setDiv(new XhtmlNode(NodeType.Element)
                        .addText(jsonObject.getString("patientName").concat("的就诊")));
        enc.setText(narrative);

        enc.setStatus(Encounter.EncounterStatus.FINISHED);
        //就诊类型
        enc.setClass_(new Coding()
                .setSystem("http://hl7.org/fhir/v3/ActCode")
                .setCode(jsonObject.getString("visitTypeCode"))
                .setDisplay(jsonObject.getString("visitTypeName")));

        enc.setSubject(new Reference()
                .setDisplay(jsonObject.getString("patientName")));

        //主治医生
        enc.addParticipant()
                .addType(new CodeableConcept()
                        .addCoding(new Coding("http://hl7.org/fhir/v3/ParticipationType", "ADM", "主治医生")))
                .setIndividual(new Reference().setDisplay(jsonObject.getString("participant")));
        //就诊时间
        enc.setPeriod(new Period().setStart(jsonObject.getDate("visitTime")).setEnd(jsonObject.getDate("dischargeTime")));

        //就诊原因
       /* enc.addReason(new CodeableConcept().setText(jsonObject.getString("visitReason")));*/

        //入院诊断
        enc.addDiagnosis(new Encounter.DiagnosisComponent()
                .setCondition(new Reference()
                        .setDisplay(jsonObject.getString("diagnosis")))
                /*.setRole(new CodeableConcept()
                        .addCoding(new Coding("http://hl7.org/fhir/diagnosis-role", "AD", "入院诊断")))*/
                .setRank(1)
        );
        //患者来源和去向
        enc.setHospitalization(new Encounter.EncounterHospitalizationComponent()
                .setOrigin(new Reference("Location/loc1").setDisplay(jsonObject.getString("origin")))
                .setAdmitSource(new CodeableConcept()
                        .addCoding(new Coding("http://hl7.org/fhir/admit-source",
                                jsonObject.getString("adminSourceCode"),
                                jsonObject.getString("adminSourceName"))))
                .setDischargeDisposition(new CodeableConcept()
                        .addCoding(new Coding("http://hl7.org/fhir/discharge-disposition",
                                jsonObject.getString("dischargeDispositionCode"),
                                jsonObject.getString("dischargeDispositionName"))))
        );
        //就诊医疗机构
        enc.setServiceProvider(new Reference().setDisplay(jsonObject.getString("orgName")));

        return enc;
    }
}
